﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace Shop.Implementation.SO
{
    public class SoCount
    {

        public static string SplitCondition(string couponCountStr, SqlCommand cncmd, int iMaxID/*orderID*/, string tbMain, string tbDetail, int idcus, int IDPromote = 0/*优惠活动的详情ID*/)
        {
            int iGSS = 0;
            int iGSE = 0;
            int iTLen = couponCountStr.Length;
            string rval = couponCountStr;
            int istart = 0;
            string functionName;
            string whereStr;
            string totalFunction;
            while (istart != -1)
            {
                istart = rval.IndexOf("@");
                if (istart != -1)
                {
                    iGSS = rval.IndexOf("(", istart);
                    functionName = rval.Substring(istart, iGSS - istart);
                    iGSE = rval.IndexOf(")", istart);
                    whereStr = rval.Substring(iGSS + 1, iGSE - iGSS - 1);
                    totalFunction = rval.Substring(istart, iGSE - istart + 1);
                    string ss = Count(functionName, whereStr, cncmd, iMaxID, tbMain, tbDetail, idcus, IDPromote).GetVal_String();
                    rval = rval.Replace(totalFunction, ss);
                    //MessageBox.Show(function);
                    //MessageBox.Show(whereStr);
                    //MessageBox.Show(totalFunction);

                }
            }
            return rval;
        }
        public static object Count(string funtionName, string whereStr, SqlCommand cncmd, int iMaxID, string tbMain, string tbDetail, int idcus, int IDPromote = 0/*优惠活动的详情ID*/)
        {
            switch (funtionName)
            {
                case "@订单金额":
                    return SoCount.GetSoSum(cncmd, whereStr, iMaxID, tbMain, tbDetail, idcus);
                case "@订单数量":
                    return SoCount.GetSoQuantity(cncmd, whereStr, iMaxID, tbMain, tbDetail, idcus);
                case "@下单次数":
                    return SoCount.OrderCount(cncmd, tbMain, tbDetail, idcus);
                case "@赠送次数":
                    return SoCount.GetGiftCount(cncmd, tbMain, tbDetail, idcus, IDPromote);
                default:
                    throw new Exception("公式暂未实现");
            }
        }
        private static double GetSoSum(SqlCommand cncmd, string cInvCodes, int iMaxID, string tbMain, string tbDetail, int idcus)
        {
            string cmdsql = string.Format("select SUM(iSum) from {0}  where id={1} and isnull(bcx,0)=0 ", tbDetail, iMaxID);
            if (cInvCodes.Length > 0)
            {
                cmdsql += string.Format(" and (cInvCode in ({0}) or cInvCode+'-'+cInvCodeSub in ({0}))", cInvCodes);
            }
            cncmd.CommandText = cmdsql;
            double iSoSum = (cncmd.ExecuteScalar()).GetVal_Double();
            return iSoSum;
        }
        private static double GetSoQuantity(SqlCommand cncmd, string cInvCodes, int iMaxID, string tbMain, string tbDetail, int idcus)
        {
            string cmdsql = string.Format("select SUM(iquantity) from {0}  where id={1} and isnull(bcx,0)=0 ", tbDetail, iMaxID);
            if (cInvCodes.Length > 0)
            {
                cmdsql += string.Format(" and (cInvCode in ({0}) or cInvCode+'-'+cInvCodeSub in ({0}) )", cInvCodes);
            }
            cncmd.CommandText = cmdsql;
            double iSoSum = (cncmd.ExecuteScalar()).GetVal_Double();
            return iSoSum;
        }

        private static int OrderCount(SqlCommand cncmd, string tbMain, string tbDetail, int idcus)
        {
            string cmdsql = string.Format("select count(*) from {0}  where idcus={1} and istatus>=0 ", tbMain, idcus);
            cncmd.CommandText = cmdsql;
            int iOrderCount = (cncmd.ExecuteScalar()).GetVal_Int();
            return iOrderCount;
        }
        private static int GetGiftCount(SqlCommand cncmd, string tbMain, string tbDetail, int idcus, int IDPromote)
        {
            string cmdsql = string.Format(@"   
    select COUNT(*) from  {0}
    left join {1} on {0}.ID = {1}.ID
    where  dDate = '{2}' and IDCus = {3} and IDPromote = {4} and isnull(iStatus,0)>=0
    ", "sopre", "sodetailpre", DateTime.Now.ToString("yyyy-MM-dd"), idcus, IDPromote);
            cncmd.CommandText = cmdsql;
            int iOrderCount = (cncmd.ExecuteScalar()).GetVal_Int();
            return iOrderCount;
        }

    }
}